package com.ayf.payment.game.api.gateway;

import cn.hutool.core.util.StrUtil;
import com.ayf.payment.game.api.type.CommonConstants;
import com.ayf.payment.game.api.utils.CommonResult;
import com.ayf.payment.game.api.utils.Md5Utils;
import com.ayf.payment.game.api.utils.RSA;
import com.ayf.payment.game.api.utils.SnowFlakeUtils;
import com.ayf.payment.game.api.utils.http.MyHttpUtil;
import com.swwx.charm.commons.lang.utils.LogPortal;
import org.apache.http.conn.ConnectTimeoutException;

import java.net.SocketTimeoutException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: zh-liang
 * @Date : 2020-12-10 11:06
 */
public class GatewayUtil {

    public static CommonResult<String> test(String ip, String port, Integer merchantId, String pubKey) {
        String url = "http://".concat(ip).concat(":").concat(port).concat("/cashOut/test");
        String traceId = SnowFlakeUtils.getTraceId();
        try {
            HashMap<String, Object> result = MyHttpUtil.doPostFrom(traceId, url, null);
            int code = Integer.parseInt(result.get(MyHttpUtil.STATUS_CODE).toString());
            if (code != 200) {
                LogPortal.error("{} 商户号：{} url：{} 测试网关连接失败，返回code不是200", traceId, merchantId, url);
                return new CommonResult<>(CommonConstants.RETURN_ERROR, "连接失败，请检查IP、端口");
            }
            String suc = result.get(MyHttpUtil.RESPONSE_ENTITY).toString();
            if (StrUtil.isEmpty(suc)) {
                LogPortal.error("{} 商户号：{} url：{}  测试网关连接失败，返回为空", traceId, merchantId, url);
                return new CommonResult<>(CommonConstants.RETURN_ERROR, "连接失败，请检查IP、端口");
            }
            String asePass = RSA.decrypt(suc, pubKey);
            if (StrUtil.isEmpty(asePass)) {
                LogPortal.error("{} 商户号：{} url：{}  测试网关连接成功，密匙校检失败，请检查秘钥是否一致", traceId, merchantId, url);
                return new CommonResult<>(CommonConstants.RETURN_ERROR, "连接成功，密匙校检失败，请检查秘钥是否一致");
            }
            LogPortal.info("{} 商户号：{} url：{}  测试网关连接成功", traceId, merchantId, url);
            return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "检测成功");
        } catch (SocketTimeoutException e) {
            LogPortal.error("{} 商户号：{} url：{}  测试网关连接失败，连接超时 SocketTimeoutException", traceId, merchantId, url);
            return new CommonResult<>(CommonConstants.RETURN_ERROR, "连接超时");
        } catch (Exception e) {
            LogPortal.error("{} 商户号：{} url：{}  测试网关连接失败，发生 {} 异常，异常信息为：{}", traceId, merchantId, url, e.getClass().getSimpleName(), e.getMessage());
        }
        return new CommonResult<>(CommonConstants.RETURN_ERROR, "连接失败，请检查IP、端口");
    }

    public static void pullNewOrder(String ip, String port, Integer merchantId) {
        String url = "http://".concat(ip).concat(":").concat(port).concat("/cashOut/getData");
        String traceId = SnowFlakeUtils.getTraceId();
        try {
            HashMap<String, Object> result = MyHttpUtil.doPostFrom(traceId, url, null);
            int code = Integer.parseInt(result.get(MyHttpUtil.STATUS_CODE).toString());
            if (code != 200) {
                LogPortal.error("{} 商户号：{} url：{} 拉取最新玩家提现订单失败，返回code不是200", traceId, merchantId, url);
                return;
            }
            String suc = result.get(MyHttpUtil.RESPONSE_ENTITY).toString();
            if ("success".equals(suc)) {
                LogPortal.info("{} 商户号：{} url：{}  拉取最新玩家提现订单成功", traceId, merchantId, url);
            } else {
                LogPortal.error("{} 商户号：{} url：{}  通知url：{} 拉取最新玩家提现订单失败，返回不是success，返回：{}", traceId, merchantId, url, suc);
            }
        } catch (SocketTimeoutException | ConnectTimeoutException e) {
            LogPortal.error("{} 商户号：{} url：{}  拉取最新玩家提现订单失败，连接超时 {}", traceId, merchantId, url, e.getClass().getSimpleName());
        } catch (Exception e) {
            LogPortal.error("{} 商户号：{} url：{}  拉取最新玩家提现订单失败，发生 {} 异常，异常信息为：{}", traceId, merchantId, url, e.getClass().getSimpleName(), e.getMessage());
        }
    }

    public static void main(String[] args) {
        String url = "http://".concat("192.168.20.21").concat(":").concat("9901").concat("/cashOut/notify");
        String k="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCsMJ9cs2jVDQj9D/flaSZmT45PK9EHzp9FnqKXNZoVlFL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dsVAyAiYiFcAyxAcPnmbENZySb8U1yqGp1omp+YOI9WYqaZTKhe/zJFAJJb/2r9IFcl39WcC7LFmAnz9/DXyKqTdI5W9qd6pjWOQKBgGetd+8CX2aaTzI+gUOCc6nRmVrDR83pXA5XeoHrfKsFwmvAqLL7FVOK1rqgD5nD0ykgJHDsucZvYPXJ4NuWFUO9dmfnxOivdWz23Eap7yXwGwnE+1kcippL41XgXxYXlvcte5e6jKqGaw8yVPa4DtbIGVMl3aD1YH4NS2BCSTvZAoGAKUygmR6MuFDDH6nRlyWpMPYgIeb5E22jXaDD5ApYtVeF22JzAukAmRMvjHohX+6JPzetbt/5OnrgidkEklGPUqICIRo3qijujCP431Q4TjS5i1PN60ZA9byUB1q7KRB5AJao/LIQyez4KVDwrESRi5A5YxkdsNPm7pwEmRSl1a4=";
        System.out.println(playerGatewayNotify("GT2012112019787051038573277197", "TX202012715309330439", k, url));
    }

    public static boolean playerGatewayNotify(String merchantOrderNo, String gameOrderNo, String prKey, String url) {
        String traceId = SnowFlakeUtils.getTraceId();
        try {
            Map<String, Object> map = new HashMap<>();
            long timeMillis = System.currentTimeMillis();
            map.put("gameOrderNo", gameOrderNo);
            map.put("time", timeMillis);
            map.put("sign", Md5Utils.encode(gameOrderNo.concat(String.valueOf(timeMillis)).concat(prKey)).toLowerCase());
            HashMap<String, Object> result = MyHttpUtil.doPostFrom(traceId, url, map);
            int code = Integer.parseInt(result.get(MyHttpUtil.STATUS_CODE).toString());
            if (code != 200) {
                LogPortal.error("{} 商户订单号：{} 游戏订单号：{} 通知url：{} 玩家提现订单通知网关失败，返回code不是200", traceId, merchantOrderNo, gameOrderNo, url);
                return false;
            }
            String suc = result.get(MyHttpUtil.RESPONSE_ENTITY).toString();
            if ("success".equals(suc)) {
                LogPortal.info("{} 商户订单号：{} 游戏订单号：{} 玩家提现订单通知网关成功", traceId, merchantOrderNo, gameOrderNo);
                return true;
            } else {
                LogPortal.error("{} 商户订单号：{} 游戏订单号：{} 通知url：{} 玩家提现订单通知网关失败，返回不是success，返回：{}", traceId, merchantOrderNo, gameOrderNo, url, suc);
            }
        } catch (SocketTimeoutException | ConnectTimeoutException e) {
            LogPortal.error("{} 商户订单号：{} 游戏订单号：{} 通知url：{}  玩家提现订单通知网关失败，连接超时 {}", traceId, merchantOrderNo, gameOrderNo, url, e.getClass().getSimpleName());
        } catch (Exception e) {
            LogPortal.error("{} 商户订单号：{} 游戏订单号：{} 通知url：{} 玩家提现订单通知网关失败，发生 {} 异常，异常信息为：{}", traceId, merchantOrderNo, gameOrderNo, url, e.getClass().getSimpleName(), e.getMessage());
        }
        return false;
    }
}
